package com.ray.tcc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.tcc.service.ApplicationService;
import com.ray.tcc.table.entity.Application;
import com.ray.tcc.util.DataGridUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.datastructure.TreeNode;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;


@Controller
@RequestMapping("/tcc/app")
public class AppController extends WebController{

     @Autowired
	 ApplicationService applicationService;
	 @Autowired
	 UcenterApiService ucenterApiService;
	
	
	@RequestMapping(value="view")
	@RequiresPermissions("tcc:app:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		modelAndView.setViewName("tcc/app/view");
		return modelAndView;
		
	}
	/**
	 * 新增页面
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value="addView")
	@RequiresPermissions("tcc:app:add")
	public ModelAndView addView(String menuId,Application params,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		modelAndView.addObject("record",params);
		modelAndView.setViewName("tcc/app/add");
		return modelAndView;
	}
	
	/**
	 * 应用新增
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("tcc:app:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(Application params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result= applicationService.addApplication(params,user);
		return doResult(result);
	}
	/*
	 * 应用修改页面
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("tcc:app:edit")
	@ResponseBody
	public ModelAndView updateView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		
	  	Application params=applicationService.queryApplication(id,user);
	
		modelAndView.addObject("record",params );
		modelAndView.setViewName("tcc/app/edit");
		return modelAndView;
		   
	}
	
	/**
	 * 应用修改
	 */
	@RequestMapping(value = "edit")
	@RequiresPermissions("tcc:app:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(Application params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result=applicationService.updateApplication(params,user);
		return doResult(result);
		
	}
	
	/**
	 * 应用详情页面
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("tcc:app:detail")
	@ResponseBody
	public ModelAndView detailView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
    	Result<Map<String, List<Button>>>  buttonsResult= ucenterApiService.queryUserButtonsWithMenu(user,menuId);
	  	if(buttonsResult.codeSuccess()){
	  		 modelAndView.addObject("buttons", buttonsResult.getData());
	  	}
		
	  	Application params=applicationService.queryApplication(id,user);
		modelAndView.addObject("record",params );
		modelAndView.setViewName("tcc/app/detail");
		return modelAndView;
		   
	}
	/**
	 * 应用删除页面
	 */
	@RequestMapping(value = "delete")
	@RequiresPermissions("tcc:app:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user =getLoginUser();
		Result<Object> result=applicationService.deleteApplication(id,user);
		return doSuccess(result);
		
	}
	/**
	 * 应用查询页面
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "query")
	@RequiresPermissions("tcc:app:query")
	@ResponseBody
    public ResponseEntity<Map<String,Object>> query(Application params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		PageDto<Application> page= this.createJQPage(request);
		page= applicationService.queryApplication(params,page,user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}	
	
	/**
	* @return
	*/
	@RequestMapping(value = "appTree")
	@ResponseBody
	public ResponseEntity<List<TreeNode>> orgTree(String id,Integer level){
		LoginUser user=getLoginUser();
		if(StrUtil.isBlank(id)){
			List<TreeNode> list = applicationService.queryApplicationList(user);
			return doSuccess(list);
		}
		
		return doSuccess(new ArrayList<TreeNode>());
	}
}
